package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @auther LiZiBa
 * @date 2020/10/14 23:19
 * @description:
 **/

@RestController
@RequestMapping(value = "/upload")
@CrossOrigin
public class FileUploadController {


    @PostMapping
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception{


        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),
                file.getBytes(),
                StringUtils.getFilenameExtension(file.getOriginalFilename()));

        String[] uploads = FastDFSUtil.upload(fastDFSFile);

        String url = FastDFSUtil.getTrackerInfo() + "/" + uploads[0]+"/"+uploads[1];

        return new Result(true, StatusCode.OK, "文件上传成功！", url);
    }
}
